﻿/*******************************************************************
Copyright (c) 2011 이재범

*  Permission is hereby granted, free of charge, to any person
*  obtaining a copy of this software and associated documentation
*  files (the "Software"), to deal in the Software without
*  restriction, including without limitation the rights to use,
*  copy, modify, merge, publish, distribute, sublicense, and/or sell
*  copies of the Software, and to permit persons to whom the
*  Software is furnished to do so, subject to the following
*  conditions:
* 
*  The above copyright notice and this permission notice shall be
*  included in all copies or substantial portions of the Software.
* 
*  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
*  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
*  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
*  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
*  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
*  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
*  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
*  OTHER DEALINGS IN THE SOFTWARE.
*******************************************************************/

#include "data.h"
#include <fstream>
#include <iostream>
using namespace std;

void Data::load_data(char delimiter)
{
	ifstream get_data(store_location);

	int temp;
	string temp_str;

	int location_count = -1;

	// 파일에서 data_location 번째의 데이터를 얻어온다. 
	// 이 때 data_location 이 -1 이면 그냥 전체 데이터를 가져온다. 
	if(data_location != -1)
	{
		while (get_data.good())
		{
			get_data >> temp; 
			if(temp == delimiter)
			{
				location_count ++;

				if(location_count == data_location)
					break;
				continue;
			}
			if(temp == EOF)
				break;

			if(location_count == data_location - 1)
			{
				temp_str.push_back(static_cast<char>(temp));
			}
		}
	}
	else
	{
		while (get_data.good())
		{
			get_data >> temp; 
			if(temp == EOF)
				break;
			temp_str.push_back(static_cast<char>(temp));
		}

	}
	data.clear();
	data = temp_str;
}
Data::Data()
{
}
Data::Data(string _store_location)
{
	store_location = _store_location;
}
void Data::load_data()
{
	char c[max_data_size];
	ifstream get_data(store_location);
	while (get_data.good())
	{
		get_data.getline(c, max_data_size);
		data.append(c);
	}

	get_data.close();
}
void Data::load_tex_data()
{
	char c[max_data_size];
	ifstream get_data(store_location);
	while (get_data.good())
	{
		get_data.getline(c, max_data_size);
		for(int i = 0; i < static_cast<int>(strlen(c)); i++)
		{
			if(c[i] == '%')
			{
				if(i == 0 || c[i - 1] != '\\')
				{
					c[i] = NULL;
				}
			}
		}
		data.append(c);
	}

	get_data.close();
}
void Data::save_data()
{
	ofstream file_out;
	file_out.open(store_location);

	file_out << data;

	file_out.close();
}
void Data::append_data(string _save_location)
{
	ofstream file_out;
	file_out.open(_save_location, ios::out | ios::app);

	if(!file_out)
	{
		cout << "ERROR : " << _save_location << endl;
		return;
	}
	file_out << data;

	file_out.close();
}
